package com.greenline.push.home.controllers.task;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.dal.base.PageQuery;
import com.greenline.push.services.CheckServiceStatusService;
import com.greenline.push.services.dto.QueueDTO;
import com.greenline.push.services.dto.ResponseCode;
import com.greenline.push.services.dto.ResponseDTO;
import com.greenline.push.web.share.ReceiveStatisticsService;

@Controller
@RequestMapping(value = "/queue")
public class QueueStatusController {
    @Resource
    private CheckServiceStatusService checkService;
    @Resource
    private ReceiveStatisticsService receiveService;
    private final Log logger = LogFactory.getLog(QueueStatusController.class);

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        return "queue/index";
    }

    @GuserResource("appchannel从guser获取")
    @RequestMapping(value = "/list-all.json")
    public @ResponseBody
    GridJsonData<QueueDTO> listAll(ModelMap model, @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        List<QueueDTO> partnerList = new ArrayList<QueueDTO>();
        int index = Integer.parseInt(pageIndex);
        int size = Integer.parseInt(pageSize);
        GridJsonData<QueueDTO> ret = new GridJsonData<QueueDTO>();
        List<QueueDTO> retursnList = checkService.checkAllQueueStatus().getDataResult();
        if (retursnList != null) {
            for (int i = (index - 1) * size; (i < (index - 1) * size + size) && i < retursnList.size(); i++) {
                partnerList.add(retursnList.get(i));
            }
        }
        PageQuery<QueueDTO> query = new PageQuery<QueueDTO>();
        query.setDataList(partnerList);
        query.setTotalRecord(retursnList.size());
        query.setPageIndex(index);
        query.setPageSize(size);
        ret.setQuery(query);
        return ret;
    }

    @RequestMapping(value = "/statistics")
    public String getStatistics(ModelMap map) {
        ConcurrentHashMap<String, List<QueueDTO>> sData = receiveService.getStatisticsData();

        map.put("queue", sData);

        return "queue/statistics";
    }

    @RequestMapping(value = "/clear", method = RequestMethod.GET)
    public @ResponseBody
    BaseJson add(ModelMap model) {
        BaseJson json = new BaseJson();
        try {
            receiveService.clearStatus();
            json.setHasError(false);
            json.setMessage("清理成功");
        } catch (Exception e) {
            e.printStackTrace();
            json.setHasError(true);
            json.setMessage("清理失败");
            logger.error(e.getMessage());
        }
        return json;
    }

    @GuserResource("关闭统计任务")
    @RequestMapping(value = "/stopTask", method = RequestMethod.GET)
    public @ResponseBody
    BaseJson stopTask(ModelMap model) {
        BaseJson json = new BaseJson();

        ResponseDTO<Boolean> r = checkService.stopAllStatistics();
        if (r.getCode().equalsIgnoreCase(ResponseCode.SUCCESS)) {
            json.setHasError(true);
            json.setMessage("任务关闭成功");
        } else {
            json.setHasError(false);
            json.setMessage("任务关闭失败");
        }
        return json;
    }

    @GuserResource("开启统计任务")
    @RequestMapping(value = "/startTask", method = RequestMethod.GET)
    public @ResponseBody
    BaseJson startTask(ModelMap model) {
        BaseJson json = new BaseJson();
        ResponseDTO<Boolean> r = checkService.startAllStatistics();
        if (r.getCode().equalsIgnoreCase(ResponseCode.SUCCESS)) {
            json.setHasError(true);
            json.setMessage("任务关闭成功");
        } else {
            json.setHasError(false);
            json.setMessage("任务关闭失败");
        }
        return json;
    }
}
